package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.utils.RDateFormatUtils;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.domain.Product;
import com.rockcent.mall.lib.domain.enumclass.ProductSourceType;
import com.rockcent.mall.lib.domain.enumclass.ProductType;
import com.rockcent.mall.lib.domain.enumclass.StatististicsType;
import com.rockcent.mall.lib.dto.EvaluationDto;
import com.rockcent.mall.lib.dto.ProductDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.EvaluationRepository;
import com.rockcent.mall.lib.repository.MerchantProductRepository;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by DK on 16/12/19.
 * 商家产品评价
 */
@RestController
public class MerchantProductEvaluationController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MerchantProductEvaluationController.class);

    @Autowired
    private BaseUserHelper userHelper;

    @Autowired
    private EvaluationRepository evaluationRepository;

    @Autowired
    private MerchantProductRepository merchantProductRepository;

    /**
     * @api {get} /merchant/product/evaluation/page 订单产品评价 分页
     * @apiGroup PRODUCT_EVALUATION
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {String} keyword 产品信息/订单编号
     * @apiParam {String=ALL("所有类型"), GOODS("商品"), SERVICE("服务")} productType <code>必要参数</code> 产品类型 默认传ALL
     * @apiParam {Long=0("所有星级"), 1("一星"), 2("二星"), 3("三星"), 4("四星"), 5("五星")} rangeStar 评分 <code>必要参数</code> 默认传0
     * @apiParam {date} beginDate 开始时间
     * @apiParam {date} endDate 结束时间
     * @apiParam {String=YESTERDAY("昨天"), WEEK("最近一周"), MONTH("最近一个月"), NONE("日期范围")} statisticsType <code>必要参数</code> 查询时间类型 默认传MONTH, 日期范围 不为空时,传NONE
     *
     * @apiSuccess {Long} id 评价ID
     * @apiSuccess {date} dateCreated 评价时间
     * @apiSuccess {String} comment 评价内容
     * @apiSuccess {String} commentCustomerName 评价人
     * @apiSuccess {Long} productId 产品编号
     * @apiSuccess {String} productName 产品名称
     * @apiSuccess {String} merchantOrderNum 订单编号
     * @apiSuccess {Long} rangeStar 评分
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *      "code": "1",
     *      "desc": "成功",
     *      "data": {
     *          [
     *        {
     *          "id": 1012,
     *          "dateCreated": "2016-12-01 14:08:23",
     *          "lastUpdated": "2016-12-20 10:48:50",
     *          "comment": "好评！",
     *          "commentCustomerName": "管彤",
     *          "customerId": 17,
     *          "merchantId": 131,
     *          "productId": 1012,
     *          "productName": "简约大气OL风打底衫",
     *          "rangeStar": 5,
     *          "merchantOrderNum": "342352345234",
     *          "hideName": true
     *      },
     *      ......
     *      ]
     * }
     */
    @RequestMapping(value = Routes.MERCHANT_PRODUCT_EVALUATION_PAGE, method = RequestMethod.GET)
    private JsonResult productEvaluationPage (HttpServletRequest request, EvaluationDto dto) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {

            if (dto.getRangeStar() == null) {
                jsonResult.remind("评分星级只允许 0(\"所有星级\"), 1(\"一星\"), 2(\"二星\"), 3(\"三星\"), 4(\"四星\"), 5(\"五星\")",log);
                return;
            }

            if (StringUtils.isBlank(dto.getProductType()) || !EnumUtils.isValidEnum(ProductType.class, dto.getProductType())) {
                jsonResult.remind("产品类型只允许 ALL(\"所有类型\"), GOODS(\"商品\"), SERVICE(\"服务\")",log);
                return;
            }
            if (StringUtils.isBlank(dto.getStatisticsType()) || !EnumUtils.isValidEnum(StatististicsType.class, dto.getStatisticsType())) {
                jsonResult.remind("statisticsType 必须为 YESTERDAY(\"昨天\"), WEEK(\"最近一周\"), MONTH(\"最近一个月\")",log);
                return;
            }
            //默认查询时间为近一个月
            if (StatististicsType.MONTH == StatististicsType.valueOf(dto.getStatisticsType())) {
                dto.setBeginDate(RDateFormatUtils.formatParseDatePattern(RDateUtils.beforeDate(30)));
                dto.setEndDate(RDateFormatUtils.formatParseDatePattern(new Date()));
            }

            if (StatististicsType.YESTERDAY == StatististicsType.valueOf(dto.getStatisticsType())) {
                dto.setBeginDate(RDateFormatUtils.formatParseDatePattern(RDateUtils.beforeDate(1)));
                dto.setEndDate(RDateFormatUtils.formatParseDatePattern(new Date()));
            }

            if (StatististicsType.WEEK == StatististicsType.valueOf(dto.getStatisticsType())) {
                dto.setBeginDate(RDateFormatUtils.formatParseDatePattern(RDateUtils.beforeDate(7)));
                dto.setEndDate(RDateFormatUtils.formatParseDatePattern(new Date()));
            }

            PageResult<EvaluationDto> pageResult = evaluationRepository.findProductEvaluationPage(userInfoDto.mall.getId(), userInfoDto.merchant.getId(), dto.getRangeStar(), dto.getBeginDate(), dto.getEndDate(), dto.getProductType(), dto.getKeyword(), this.getPage(dto.getPageNo(), dto.getPageSize()));
            pageResult.setUrl(Routes.MERCHANT_PRODUCT_EVALUATION_PAGE);
            jsonResult.data = pageResult;
        };
        return doing.go(request, log);
    }

    /**
     * @api {get} /merchant/product/evaluation/statistics/page 订单产品评价统计 分页
     * @apiGroup PRODUCT_EVALUATION
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {String} keyword 产品信息/产品编号
     * @apiParam {String=SELF("自营"), MARKET("分销"), ALL("全部")} productSourceType <code>必要参数</code> 产品来源 默认传ALL
     * @apiParam {String=ASC("升序"), DESC("降序")} sort 排序(sortByName为空则sort为空,sortByName传值则sort传值)
     * @apiParam {String=goodRate("好评率"), goodNew("好评数"), middleNew("中评数"), badNew("差评数")} sortByName 按字段排序
     *
     * @apiSuccess {Long} id 评价ID
     * @apiSuccess {String} name 产品名称
     * @apiSuccess {String} goodRate 好评率 不为空,除100,加上%符号, 为空,默认100%
     * @apiSuccess {String} goodNew 好评数
     * @apiSuccess {String} middleNew 中评数
     * @apiSuccess {String} badNew 差评数
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *      "code": "1",
     *      "desc": "成功",
     *      "data": {
     *          [
     *        {
     *          "id": 1012,
     *          "productId": 1012,
     *          "name": "简约大气OL风打底衫",
     *          "goodRate": "0.213"
     *          "goodNew": "12"
     *          "middleNew": "12"
     *          "badNew": "32"
     *      },
     *      ......
     *      ]
     * }
     */
    @RequestMapping(value = Routes.MERCHANT_PRODUCT_EVALUATION_STATISTICS_PAGE, method = RequestMethod.GET)
    public JsonResult productEvaluationStatisticsPage (HttpServletRequest request, ProductDto dto) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (StringUtils.isNotBlank(dto.getSortByName()) && StringUtils.isBlank(dto.getSort())) {

            }
            if (StringUtils.isBlank(dto.getSortByName()) && StringUtils.isNotBlank(dto.getSort())) {

            }
            if (StringUtils.isBlank(dto.getProductSourceType()) || !EnumUtils.isValidEnum(ProductSourceType.class, dto.getProductSourceType())) {
                jsonResult.remind("产品来源只允许 ALL(\"所有全部\"), SELF(\"自营\"), MARKET(\"分销\")",log);
                return;
            }
            PageResult<ProductDto> pageResult = merchantProductRepository.findProductEvaluationStatisticsPage(userInfoDto.mall.getId(), userInfoDto.merchant.getId(), dto.getProductSourceType(), dto.getKeyword(), dto.getSortByName(), dto.getSort(), this.getPage(dto.getPageNo(), dto.getPageSize()));
            pageResult.setUrl(Routes.MERCHANT_PRODUCT_EVALUATION_STATISTICS_PAGE);
            jsonResult.data = pageResult;
        };
        return doing.go(request, log);
    }
}
